package org.cornland.systemWarning.schedule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.cornland.systemWarning.model.DataExceptionAlarm;
import org.cornland.systemWarning.model.Well;
import org.cornland.systemWarning.service.DataExceptionAlarmService;
import org.cornland.systemWarning.service.WellService;
import org.cornland.systemWarning.utils.AppUrl;
import org.cornland.systemWarning.utils.GZIPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class InternetSchedule {

    @Autowired
    AppUrl appUrl;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    WellService wellService;

    @Autowired
    DataExceptionAlarmService dataExceptionAlarmService;

//    @Scheduled(cron = "0 1,11,21,31,41,51 * * * ?") 唐思豪写的，他没有放开，估计没用
    public void InternetTrafficSchedule(){
        try {
            log.info("通信异常检查开始");
            InternetTraffic();
            log.info("通信异常检查结束");
        } catch (RestClientException e){
            log.error("请求接口超时,请检查");
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            log.error("通信异常遇检查到问题");
            log.error(e.getMessage(), e);
        }
    }

    public void InternetTraffic(){
        String url = appUrl.queryRealTimeDataUrl + "?tags=" + "A22211BPLC0110010";
        URI uri = URI.create(url);
        String tagString = restTemplate.getForObject(uri, String.class);
        JSONArray jsonArray = JSON.parseArray(tagString);
        if(jsonArray.size()!=0){
            Map<String,Object> object = jsonArray.getObject(0, Map.class);
            // 取得value
            String value = (String) object.get("value");
            // 取得时间作为记录时间
            String time = (String) object.get("time");

            // 解压value
            String uncompress = GZIPUtils.uncompress(value);
            Map<String,Object> map = JSON.parseObject(uncompress, Map.class);
            // 取得其中ping有问题的井的list
            List<String> pingWellList = (List) map.get("pingWellList");
            for (String serialNo : pingWellList){
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("serialNo",serialNo);
                Well well = wellService.getOne(wrapper);
                if(well!=null){
                    String jmjh = well.getJmjh();
                    String message = jmjh+" 网络通信异常";
                    log.warn(message);
                    DataExceptionAlarm dataExceptionAlarm = new DataExceptionAlarm();
                    dataExceptionAlarm.setWellId(well.getId());
                    dataExceptionAlarm.setTime(time);
                    dataExceptionAlarm.setFlag(0);
                    dataExceptionAlarm.setType(2);
                    dataExceptionAlarm.setDescription("网络通信异常");
                    dataExceptionAlarmService.save(dataExceptionAlarm);
                }
            }
        }

    }

}
